package com.dong.mcd.control;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.dong.mcd.model.ReportDimension;
import com.dong.mcd.pub.BaseAction;
import com.dong.mcd.service.ICommService;
import com.dong.mcd.service.IReportService;

@Controller
@RequestMapping("/report")
public class ReportAction extends BaseAction {

	@Autowired
	private ICommService commService;
	@Autowired
	private IReportService reportService;
	
	@RequestMapping("/getColumn")
	public void getColumn(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String sql  = request.getParameter("sql");
		writeJson(response, commService.queryColumnBySql(sql));
	}
	
	@RequestMapping("/toDimResultList")
	public String toDimResultList(HttpServletRequest request, HttpServletResponse response){
		String sql  = request.getParameter("sql");
		try {
			if (StringUtils.isNotEmpty(sql))
				request.setAttribute("columnList", commService.queryColumnBySql(sql));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "/web/report/dim-datagrid.jsp";
	}
	
	@RequestMapping("/queryBySql")
	public void queryBySql(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String sql  = request.getParameter("sql");
		try{
			writeJson(response, commService.executeSql(sql));
		}catch (Exception e) {
			writeError(response, e.getMessage());
			e.printStackTrace();
		}
	}
	
	@RequestMapping("/queryAllDim")
	public void queryAllDim(HttpServletRequest request, HttpServletResponse response) throws Exception {
		try{
			writeJson(response, reportService.querAllDims());
		}catch (Exception e) {
			writeError(response, e.getMessage());
			e.printStackTrace();
		}
	}
	
	@RequestMapping("/saveDim")
	public void saveDim(HttpServletRequest request, HttpServletResponse response,ReportDimension param) throws Exception {
		try{
			if (param.getId() == null){
				reportService.addDim(param);
			}else{
				reportService.updateDim(param);
			}
			writeSuccess(response, "保存成功");
		}catch (Exception e) {
			writeError(response, e.getMessage());
			e.printStackTrace();
		}
	}
	
	@RequestMapping("/getDimByKey")
	public void getDimByKey(HttpServletRequest request, HttpServletResponse response,ReportDimension dimension) throws Exception {
		try{
			writeJson(response, reportService.getDimByKey(dimension.getId()));
		}catch (Exception e) {
			writeError(response, e.getMessage());
			e.printStackTrace();
		}
	}
}
